<%--
  字典类型维护界面
  User: wangyang
  Date: 12-11-6
  Time: 下午1:30
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/common/tag/taglibs.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>字典类型列表</title>
    <link rel="stylesheet" type="text/css" href="<c:url value='/js/easyUI/themes/default/easyui.css' />"/>
    <link rel="stylesheet" type="text/css" href="<c:url value='/js/easyUI/themes/icon.css' />"/>
    <script type="text/javascript" src="<c:url value='/js/jquery.min.js' />"></script>
    <script type="text/javascript" src="<c:url value='/js/easyUI/jquery.easyui.min.js' />"></script>
    <script type="text/javascript" src="<c:url value='/js/easyUI/locale/easyui-lang-zh_CN.js' />"></script>
    <link rel="stylesheet" type="text/css" href="<c:url value='/css/query_list.css' />"/>
    <script type="text/javascript" src="<c:url value='/js/validatebox.js' />"></script>
    <script type="text/javascript" src="<c:url value='/js/message.js' />"></script>
    <style type="text/css">
        .columnTitle {
            font-weight:bold;
        }
    </style>
    <script type="text/javascript">
        // 上一次的行标识
        var lastIndex;
        // 页面加载初始化方法
        $(function () {
            initDataGrid();
        });
        // 数据表格初始化方法
        function initDataGrid() {
            $('#displayTable').datagrid({
                fit:true, //表格自适应
                fitColumns:true, //列自适应
                nowrap:true, //是否换行
                autoRowHeight:false, //自动行高
                striped:true,
                collapsible:true, //是否可折叠
                url:'<c:url value="/system/queryCodeClass.do" />',
                sortName:'orderId',
                sortOrder:'asc',
                remoteSort:false,
                idField:'roleId',
                singleSelect:false, //是否单选
                pagination:true, //分页控件
                rownumbers:true, //行号
                scrollbarSize:0,
                loadMsg:"数据加载中.....",
                rowStyler:function (index, row) {
                    if (row.deltag == 1) {
                        return 'color:#748494;';
                    }
                },
                frozenColumns:[
                    [
                        {field:'ck', checkbox:true},
                        {field:'classId', hidden:true }
                    ]
                ],
                columns:[
                    [
                        {field:'className', title:'<span class="columnTitle">字典类型名称</span>', width:100, editor:'text'},
                        {field:'createTime', title:'<span class="columnTitle">建立时间</span>', width:80, sortable:true},
                        {field:'updateTime', title:'<span class="columnTitle">更新时间</span>', width:80, sortable:true},
                        {field:'description', title:'<span class="columnTitle">描述</span>', width:200, editor:'text'},
                        {field:'deltag', title:'<span class="columnTitle">删除标识</span>', width:40, formatter:deltagFormatter,
                            editor:{
                                type:'checkbox',
                                options:{
                                    on:'1',
                                    off:'0'
                                }
                            }
                        }
                    ]
                ],
                toolbar:[
                    {
                        text:'添加',
                        iconCls:'icon-add',
                        handler: addRow
                    },
                    {
                        text:'删除',
                        iconCls:'icon-remove',
                        handler: deleteRow
                    },
                    {
                        text:'保存',
                        iconCls:'icon-save',
                        handler: saveRow
                    },
                    {
                        text:'取消',
                        iconCls:'icon-undo',
                        handler:function () {
                            $('#displayTable').datagrid('rejectChanges');
                        }
                    },
                    {
                        text:'GetChanges',
                        iconCls:'icon-search',
                        handler:function () {
                            var rows = $('#displayTable').datagrid('getChanges');
                            alert('changed rows: ' + rows.length + ' lines');
                        }
                    }
                ],
                onBeforeLoad:function () {
                    $(this).datagrid('rejectChanges');
                },
                onDblClickRow:function (rowIndex) {
                    if (lastIndex != rowIndex) {
                        $('#displayTable').datagrid('endEdit', lastIndex);
                        $('#displayTable').datagrid('beginEdit', rowIndex);
                        lastIndex = rowIndex;
                    }
                }
            });
            //设置分页控件
            var p = $('#displayTable').datagrid('getPager');
            $(p).pagination({
                pageSize:10, //每页显示的记录条数，默认为10
                pageList:[10, 20, 30, 40, 50], //可以设置每页记录条数的列表
                beforePageText:'第', //页数文本框前显示的汉字
                afterPageText:'页    共 {pages} 页',
                displayMsg:'当前显示 {from} - {to} 条记录   共 {total} 条记录'
            });
        }

        function getRowIndex(target){
            var tr = $(target).closest('tr.datagrid-row');
            return parseInt(tr.attr('datagrid-row-index'));
        }

        function endEditRow() {
            if (lastIndex != rowIndex) {
                $('#displayTable').datagrid('endEdit', lastIndex);
            }
        }

        function editrow(target){
            var rowIndex = getRowIndex(target);
            if (lastIndex != rowIndex) {
                $('#displayTable').datagrid('endEdit', lastIndex);
            }
            $('#displayTable').datagrid('beginEdit', rowIndex);
            lastIndex = rowIndex;
        }

        function saveRow() {

            if (lastIndex != undefined) {
                $('#displayTable').datagrid('endEdit', lastIndex);
            }

            var insertRows = $('#displayTable').datagrid('getChanges','inserted');
            var updateRows = $('#displayTable').datagrid('getChanges','updated');
            var deleteRows = $('#displayTable').datagrid('getChanges','deleted');
            // 改变过的记录集合
            var changesRows = {
                inserted : [],
                updated : [],
                deleted : []
            };

            if (insertRows.length>0) {
                for (var i=0;i<insertRows.length;i++) {
                    changesRows.inserted.push(insertRows[i]);
                }
            }

            if (updateRows.length>0) {
                for (var k=0;k<updateRows.length;k++) {
                    changesRows.updated.push(updateRows[k]);
                }
            }

            if (deleteRows.length>0) {
                for (var j=0;j<deleteRows.length;j++) {
                    changesRows.deleted.push(deleteRows[j]);
                }
            }

            $.ajax({
                type: "post",
                url: "<c:url value='/system/saveCodeClass.do'/>",
                dataType:'json',
                data:{jsonStr: JSON.stringify(changesRows)},
                success: function (data) {
                    if (data.success) {
                        alert(data.message);
                    }
                }
            });
        }

        function deleteRow() {
            var rows = $('#displayTable').datagrid('getSelections');
            if (!rows || rows.length == 0) {
                alert('请选择需要删除的字典类型！');
                return;
            }

            confirm('是否确认删除选中的字典类型！', function(){
                while (rows && rows.length > 0) {
                    var index = $('#displayTable').datagrid('getRowIndex', rows[0]);
                    $('#displayTable').datagrid('deleteRow', index);
                }
            });
        }

        function addRow() {
            $('#displayTable').datagrid('endEdit', lastIndex);
            $('#displayTable').datagrid('appendRow', {
                className:'',
                description:'',
                deltag:'0'
            });
            lastIndex = $('#displayTable').datagrid('getRows').length - 1;
            $('#displayTable').datagrid('selectRow', lastIndex);
            $('#displayTable').datagrid('beginEdit', lastIndex);
        }

        function deltagFormatter(value){
            return value == '1' ? '是' : '否';
        }

    </script>
</head>
<body>
<!-- 功能列表DIV -->
<table id="displayTable"></table>
</body>
</html>